package com.wushi.lenovo.asia5b.app_ui.home.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.flexbox.FlexboxLayoutManager;
import com.mvp.base.BaseAppCompatActivity;
import com.mvp.utils.InputMethodUtils;
import com.wushi.lenovo.asia5b.R;
import com.wushi.lenovo.asia5b.app_ui.h5.SearchViewActivity;
import com.wushi.lenovo.asia5b.app_ui.home.adapter.SearchAdapter;
import com.wushi.lenovo.asia5b.app_utils.helper.CacheHelper;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by zhangyuncai on 2019/3/16.
 */
public class SearchActivity extends BaseAppCompatActivity {
    @BindView(R.id.et_search_content)
    EditText et_search_content;
    @BindView(R.id.iv_search_icon)
    ImageView iv_search_icon;
    @BindView(R.id.iv_search_delete)
    ImageView iv_search_delete;
    @BindView(R.id.rv_search)
    RecyclerView mRecyclerView;
    @BindView(R.id.ll_search_empty)
    LinearLayout ll_search_empty;
    private SearchAdapter mAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search;
    }


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

        initData();

        initEvent();
    }

    @Override
    protected void initView() {
        InputMethodUtils.showSoftInput(this, et_search_content);
        mRecyclerView.setLayoutManager(new FlexboxLayoutManager(this));
        mAdapter = new SearchAdapter(CacheHelper.addSearchKeyword(""));
        mRecyclerView.setAdapter(mAdapter);
        showEmptyView();
    }

    @Override
    protected void initData() {

    }

    private void search() {
        InputMethodUtils.closeKeyBoard(this);
        String keyword = et_search_content.getText().toString().trim();
        search(keyword);
    }

    private void search(String keyword) {
        if (!TextUtils.isEmpty(keyword)) {
            SearchViewActivity.startSearchViewActivityByKeyword(mActivity, keyword);
            mAdapter.setNewData(CacheHelper.addSearchKeyword(keyword));
            showEmptyView();
            finish();
        }
    }

    @Override
    protected void initEvent() {
        et_search_content.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                    search();
                    return true;
                }
                return false;
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String item = mAdapter.getItem(position);
                search(item);
                et_search_content.setText(item);
            }
        });
    }

    private void showEmptyView() {
        ll_search_empty.setVisibility(mAdapter.getData().size() != 0 ? View.GONE : View.VISIBLE);
    }

    @Override
    protected void onPause() {
        super.onPause();
        InputMethodUtils.closeKeyBoard(this);
    }

    @OnClick({
            R.id.iv_search_icon,
            R.id.iv_search_delete
    })
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_search_icon:
                search();
                break;
            case R.id.iv_search_delete:
                mAdapter.setNewData(CacheHelper.clearSearchKeyword());
                showEmptyView();
                break;
        }
    }
}
